<?php
	// -----------   Page Vars    ----------
	$to_root = '';
	$authorized = array(0, 1);
	$menu_selected = 0;
	
	//  ---- init
	include($to_root.'_classes/init.php');
	
	$main = new ViewDescriptor('main');
	Template::addBreadcrumb('home.php', 'Home');
	
	/* ========   INSERT CODE HERE   ======== */
	$content = new ViewDescriptor('content/main');
	
	//Todos
	$todos = $GLOBALS['Db-I']->getTodos(0, HOME_TODOS, 'tD', false); //limit todos + past =false
	if($todos != array() && !isset($todos['Error'])){
		foreach($todos as &$todo){
			$todo_t = new SubViewDescriptor('todos');
			$todo_s = new SubViewDescriptor('todo_scripts');
			
			$todo_s->addValue('id',  $todo->getId());
			
			$todo_t->addValue('id', $todo->getId());
			$todo_t->addValue('title', $todo->getTitle());
			$todo_t->addValue('description', $todo->getDescription());
			$todo_t->addValue('target_date', $todo->getTargetDate());
			$todo_t->addValue('projected_hours', $todo->getProjectedHours());
			$todo_t->addValue('creator', $todo->getCreator()->getNick());
			$todo_t->addValue('status_img', str_replace(' ', '_', $todo->getStatus()));
			$todo_t->addValue('status', $todo->getStatus(Todo::GET_STATUS_INTERFACE));
			
			$content->addSubView($todo_t);
			$content->addSubView($todo_s);
			
			unset($todo_t);
			unset($todo_s);
		}
	} else {
		$content->removeSubView('todos');
		$content->removeSubView('todo_scripts');
	}
	print_r($_SESSION);
	//Projekte
	$proj = $GLOBALS['Db-I']->getProjects(-1, 0, HOME_PROJECTS, 'id'); // ($uId $from=-1, $rows=-1, $sort='')
	if($proj != array() && !isset($proj['Error'])){
		foreach($proj as &$project){
			$proj_t = new SubViewDescriptor('projects');
			$proj_s = new SubViewDescriptor('project_scripts');
			
			$proj_s->addValue('id',  $project->getId());
			
			$proj_t->addValue('id', $project->getId());
			$proj_t->addValue('todo_on_schedule', ($project->getTodoOnSchedule()=='') ? '0':$project->getTodoOnSchedule());
			$proj_t->addValue('todo_behind_schedule', ($project->getTodoBehindSchedule()=='') ? '0':$project->getTodoBehindSchedule());
			$proj_t->addValue('todo_waiting', ($project->getTodoWaiting()=='') ? '0':$project->getTodoWaiting());
			$proj_t->addValue('todo_finished', ($project->getTodoFinished()=='') ? '0':$project->getTodoFinished());
			$proj_t->addValue('todo_new', ($project->getTodoNew()=='') ? '0':$project->getTodoNew());
			$proj_t->addValue('title', $project->getTitle());
			$proj_t->addValue('beschreibung', $project->getDescription());
			$proj_t->addValue('last_todo_date', $project->getLastTodoDate());
			$proj_t->addValue('todo_count', $project->getTodoCount());
			$proj_t->addValue('last_todo', $project->getLastTodo());
			
			$content->addSubView($proj_t);
			$content->addSubView($proj_s);
			
			unset($proj_t);
			unset($proj_s);
		}
	} else {
		$content->removeSubView('projects');
		$content->removeSubView('project_scripts');
	}
	/* ========   NORMAL STUFF   ========= */
	// Messages
	if(isset($_GET['msg']) && isset($errors) && is_array($errors) && isset($error_class) && is_array($error_class)){
    	$get_msg = explode('|', $_GET['msg']);
    	foreach($get_msg as $error){
    		if(isset($errors[$error]) && isset($error[$GLOBALS['Localization']['language']])){
    			$GLOBALS['Messages'][] = '<span class="msg_'.$error_class[$error].'">'.$errors[$error].'</span>';
    		}
    	}
    }
    // render Main Template
	Template::generateMenu($main, $menu_selected);
	Template::renderBreadCrumb($main);
	$main->addValue('messages', implode("\n", $GLOBALS['Messages']));
	$main->addValue('content', $content->render());
	$main->addValue('script', '');
	$main->addValue('style', '<link rel="stylesheet" href="'.$GLOBALS['tpl']['root'].'_template/todolist/'.$GLOBALS['Localization']['language'].'/css/todo.css" type="text/css"  media="screen"/>'.
							'<link rel="stylesheet" href="'.$GLOBALS['tpl']['root'].'_template/todolist/'.$GLOBALS['Localization']['language'].'/css/projekte.css" type="text/css"  media="screen"/>');
	
	echo $main->render(); 
?>